package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MenuDao;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.pojo.Menu;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 用户服务
 */

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    @Autowired
    private MenuDao menuDao;
    //根据用户名查询用户信息，同时要查询用户关联的角色、角色关联的权限
    public User findUserByUsername(String username) {
        //只查询用户表信息
        User user = userDao.findByUsername(username);
        if(user == null){
            return null;
        }

        //根据用户id查询关联的角色
        Integer userId = user.getId();
        Set<Role> roles = roleDao.findByUserId(userId);

        if(roles != null && roles.size() > 0){
            user.setRoles(roles);
            for (Role role : roles) {
                Integer roleId = role.getId();
                //根据查询到的角色id查询关联的权限信息
                Set<Permission> permissions = permissionDao.findByRoleId(roleId);
                role.setPermissions(permissions);
            }
        }

        return user;
    }

    @Override
    public List<Menu> getMenuData(String username) {
        User user = userDao.findByUsername(username);
        Integer id = user.getId();
        Set<Role> roles = roleDao.findByUserId(id);
        List<Menu> menuList = new ArrayList<>();
        for (Role role : roles) {
            Integer roleId = role.getId();
            //根据roleId遍历获取所有Menu.
            LinkedHashSet<Menu> menus = menuDao.findByRoleId(roleId);
            for (Menu menu : menus) {
                Integer parentMenuId = menu.getParentMenuId();
                if (parentMenuId==null) {
                    Integer menuId = menu.getId();
                    List<Menu> childrens = menuDao.findByMenuId(menuId);
                    menu.setChildren(childrens);
                    menuList.add(menu);

                }
            }
        }
        return menuList;
    }

    @Override
    public void add(User user, Integer[] roleIds) {
        userDao.add(user);
        Integer userId = user.getId();
        this.setUserAndRole(userId,roleIds);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<User> users = userDao.pageQuery(queryPageBean.getQueryString());
        return new PageResult(users.getTotal(),users.getResult());
    }

    @Override
    public List<Role> findAll() {
        List<Role> list = userDao.findAll();
        return list;
    }

    @Override
    public void edit(User user, Integer[] roleIds) {
        userDao.edit(user);
        userDao.deleteAssociationByRoleId(user.getId());
        this.setUserAndRole(user.getId(),roleIds);
    }

    @Override
    public User findById(Integer id) {
        return userDao.findById(id);
    }

    @Override
    public Integer[] findroleIdsByUserId(Integer id) {
        return userDao.findroleIdsByUserId(id);
    }

    @Override
    public void delete(Integer id) {
        userDao.deleteUserByRoleIds(id);
        userDao.delete(id);
    }

    public void setUserAndRole(Integer userId,Integer[] roleIds){
        if(roleIds!=null || roleIds.length>0){
            Map<String,Integer> map = new HashMap<>();
            for (Integer roleId : roleIds) {
                map.put("userId",userId);
                map.put("roleId",roleId);
                userDao.setUserAndRole(map);
            }
        }
    }
}
